<?php 
namespace Home\Controller;
use Think\Controller;
use Home\Controller\CommonController;
class UserController extends CommonController
{   
    public function uploadface()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        
        if(!$info) {
        // 上传错误提示错误信息
            echo $upload->getError();
        }else{// 上传成功
            $face = '/Uploads/'.$info['upload_file']['savepath'].$info['upload_file']['savename'];
            $id = $_SESSION['id'];

            $sql = "update user set face='{$face}' where id='{$id}'";

            $res = M()->execute($sql);

            $_SESSION['face']=$face;
            if($res){
                echo $face;
            }else{
                echo '1';
            }
        }
    }

     public function face()
    {
        $this->display();       
    }

    public function show()
    {	
        $this->display();     
    }

    public function about()
    {   
        // 查询个人资料
        $id = $_SESSION['id'];
        $sql = "select * from user where id='{$id}' ";
        $res = M()->query($sql);
        $this->assign('res',$res);
        
        // 查询个人学历
        $sql = "select * from edu where id='{$id}' ";
        $edu = M()->query($sql);
        $this->assign('edu',$edu);
        $this->display();
    }
    public function settings()
    {   
       if(IS_POST){
            $id = $_SESSION['id'];
            $data = $_POST;
            $sql = "update user set username='{$data['username']}', truename='{$data['truename']}',sex='{$data['sex']}',slug='{$data['slug']}' where id = '{$id}'";
            $res = M()->execute($sql);
            $this->success('修改成功',U('home/user/settings'),1);
        }else{
           $this->display(); 
        }
    }

    public function experience()
    {   
        if(IS_POST){
            $data = $_POST;
            $res = M('experience')->add($data);
            $this->success('修改成功',U('home/user/settings'),1);
        }else{
           $this->display(); 
        }
    }

    public function edu()
    {
        if(IS_POST){
            $data = $_POST;
            $res = M('edu')->add($data);
            $this->success('修改成功',U('home/user/settings'),1);
        }else{
           $this->display(); 
        }
    }


    public  function Email()
    {

        // if(IS_POST){
        //     $id = $_SESSION['id'];
        //     $data = $_POST;
        //     $sql = "update user set email='{$data['email']}' where id = '{$id}'";
        //     $res = M()->execute($sql);
        //     $this->success('修改成功',U('home/user/settings'),1);
        // }else{
           $this->display(); 
        // }
    }
    // 修改密码
    public function passwd()
    {
        $id = $_SESSION['id']; 
        // 用户输入的的新密码：
        $data =$_POST;
        // $aa = md5($data['newpassword']);
        $aa = md5($data['newpassword']);
        $bb = md5($data['confirmPassword']);
        // 用户输入的就密码
        
        $str = md5($_POST['password']);
        $password = $str;
        // 拿数据库旧密码
        $sql = "select * from user where password='$password' and id='$id'";
        // var_dump($sql);
        $res = M()->query($sql);

        if ($res) {
            if ($aa == $bb) {
                $sql = "update user set password='{$bb}' where id = '{$id}' ";
                $res = M()->execute($sql);
                $this->success('修改成功',U('home/user/settings'),2);
            }else{
                $this->error('两次密码不一样',U('home/user/password'),2);
            }   
        }else{
                $this->error('原始密码错误',U('home/user/password'),2);
        }
    
    }

    public function checkpassword()
    {
        $passwordd = I('post.passwordd','');

        if ($passwordd) {
            $preg = "/^[a-z0-9]{6,33}$/";
            $c = preg_match($preg, $passwordd);

            if (!$c) {
                $arr = array(
                    'error'=>1,
                    'info' =>'密码是不少于6位'
                    );
                echo json_encode($arr);
            }else{
                $arr = array(
                    'error'=>0,
                    'info' =>'密码可以用'
                    );
                echo json_encode($arr);
            }
        }else{
            $arr = array(
                    'error'=>1,
                    'info' =>'密码不可以为空'
                    );
                echo json_encode($arr);
        };
        
    }

    public function checkmail()
    {

        if(IS_POST){
            $id = $_SESSION['id'];
            $data = $_POST;
            var_dump($data);
            $sql = "update user set email='{$data['mail']}' where id = '{$id}'";
            $res = M()->execute($sql);
            var_dump($res);
            $this->success('修改成功',U('home/login/login'),1);
        }else{
           $this->display(); 
        }
    }
    
} ?>